package org.jaaksi.demo.ptr;

import android.app.Application;
import android.graphics.Bitmap;
import android.net.Uri;
import com.jakewharton.picasso.OkHttp3Downloader;
import com.orhanobut.logger.Logger;
import com.squareup.picasso.Picasso;
import com.zhy.http.okhttp.OkHttpUtils;
import okhttp3.OkHttpClient;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by Fcy on 2016/3/17.<br/>
 */
public class MyApplication extends Application
{
	private Picasso.Listener picassoListener = new Picasso.Listener()
	{
		@Override
		public void onImageLoadFailed(Picasso picasso, Uri uri,
	            Exception exception)
		{
			Logger.i("Picasso failure: %s \n    path = %s",
	                exception.toString(), uri);
		}
	};

	@Override
	public void onCreate()
	{
		super.onCreate();
		OkHttpClient client = OkHttpUtils.getInstance().getOkHttpClient();
		// client.authenticator()
		client.newBuilder().connectTimeout(30000, TimeUnit.SECONDS);
		// 如何封装公共的header，params

		// 统一配置，单例
		Picasso picasso = new Picasso.Builder(this)
		        .downloader(new OkHttp3Downloader(this))
		        .executor(Executors.newCachedThreadPool())
		        .listener(picassoListener)
		        .defaultBitmapConfig(Bitmap.Config.ARGB_8888).build();

		picasso.setLoggingEnabled(true);
		picasso.setIndicatorsEnabled(true);
		Picasso.setSingletonInstance(picasso);

		// 使用方式如下
//		Picasso picasso = Picasso.with(mContext);
//		// picasso.setIndicatorsEnabled(true);
//		// picasso.setLoggingEnabled(true);
//		//use picasso.fit().centerCrop() to resize image
//		picasso.load(new File(thumbUrl))
//			.placeholder(R.drawable.icon_gridview_picture_normal)
//			.error(R.drawable.icon_gridview_picture_normal)
//			.networkPolicy(NetworkPolicy.NO_CACHE).noFade().fit()
//			.centerCrop().tag(thumbUrl).into(holder.iv_pic);
	}
}
